﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using YArchitech.DataModel;
using YArchitech.LIB;

namespace YArchitech.Plumbing
{
	internal class HYPumpDataManager
	{
		public static List<HYPumpFactory> GetAllFactory()
		{
			List<HYPumpFactory> list = new List<HYPumpFactory>();
			foreach (object obj in new HYPumpDataReader().GetFactoryTable().Rows)
			{
				DataRow dataRow = (DataRow)obj;
				string sImage2D = Path.Combine(Product.FamilyImageLocation, "Pump", dataRow[ConstantValue.Image2D].ToString());
				string sImage3D = Path.Combine(Product.FamilyImageLocation, "Pump", dataRow[ConstantValue.Image3D].ToString());
				list.Add(new HYPumpFactory(dataRow[ConstantValue.Factory].ToString(), dataRow[ConstantValue.TypeInFactory].ToString(), dataRow[ConstantValue.TableName].ToString(), dataRow[ConstantValue.Family].ToString(), sImage2D, sImage3D));
			}
			if (list.Count >= 1)
			{
				return list;
			}
			return null;
		}

		public static List<HYPumpFactory> GetTheFactory(string szFactory)
		{
			List<HYPumpFactory> list = new List<HYPumpFactory>();
			foreach (HYPumpFactory hypumpFactory in HYPumpDataManager.GetAllFactory())
			{
				if (hypumpFactory.szFactory.CompareTo(szFactory) == 0)
				{
					list.Add(hypumpFactory);
				}
			}
			if (list.Count >= 1)
			{
				return list;
			}
			return null;
		}

		public static HYPumpFactory GetFactory(string szFactory, string szTypeInFactory)
		{
			foreach (HYPumpFactory hypumpFactory in HYPumpDataManager.GetAllFactory())
			{
				if (hypumpFactory.szFactory.CompareTo(szFactory) == 0 && hypumpFactory.szTypeInFactory.CompareTo(szTypeInFactory) == 0)
				{
					return hypumpFactory;
				}
			}
			return null;
		}

		public static List<HYPumpData> GetTablePumpBasicData(HYPumpFactory pumpFactory)
		{
			List<HYPumpData> list = new List<HYPumpData>();
			foreach (object obj in new HYPumpDataReader().GetPumpDataTable(pumpFactory.szTableName).Rows)
			{
				DataRow dataRow = (DataRow)obj;
				HYPumpData hypumpData = new HYPumpData();
				hypumpData.StrFactory = pumpFactory.szFactory;
				hypumpData.StrTypeInFactory = pumpFactory.szTypeInFactory;
				hypumpData.StrSpecification = dataRow[ConstantValue.SPEC].ToString();
				hypumpData.StrBaseType = dataRow[ConstantValue.Type].ToString();
				hypumpData.StrStructType = dataRow[ConstantValue.StructType].ToString();
				hypumpData.StrFitType = dataRow[ConstantValue.FixType].ToString();
				hypumpData.strIdentifier = HYPumpDataManager.GetTheUniqueIdentifier(pumpFactory.szTableName, hypumpData.StrSpecification);
				hypumpData.DQ = Convert.ToDouble(dataRow[ConstantValue.Volume].ToString());
				hypumpData.DH = Convert.ToDouble(dataRow[ConstantValue.Head].ToString());
				hypumpData.DEffection = Convert.ToDouble(dataRow[ConstantValue.Efficient].ToString());
				hypumpData.DPumpPower = Convert.ToDouble(dataRow[ConstantValue.Power].ToString());
				hypumpData.StrUse = dataRow[ConstantValue.Use].ToString();
				list.Add(hypumpData);
			}
			return list;
		}

		public static bool GetSpecPumpData(ref HYPumpData pumpdata)
		{
			if (string.IsNullOrEmpty(pumpdata.StrFactory) || string.IsNullOrEmpty(pumpdata.StrTypeInFactory) || string.IsNullOrEmpty(pumpdata.StrSpecification))
			{
				return false;
			}
			DataTable pumpDataTable = new HYPumpDataReader().GetPumpDataTable(HYPumpDataManager.GetFactory(pumpdata.StrFactory, pumpdata.StrTypeInFactory).szTableName);
			HYPumpFactory factory = HYPumpDataManager.GetFactory(pumpdata.StrFactory, pumpdata.StrTypeInFactory);
			string szTableName = factory.szTableName;
			foreach (object obj in pumpDataTable.Rows)
			{
				DataRow dataRow = (DataRow)obj;
				if (dataRow[ConstantValue.SPEC].ToString().CompareTo(pumpdata.StrSpecification) == 0)
				{
					pumpdata.strIdentifier = HYPumpDataManager.GetTheUniqueIdentifier(szTableName, pumpdata.StrSpecification);
					pumpdata.StrBaseType = dataRow[ConstantValue.Type].ToString();
					pumpdata.StrStructType = dataRow[ConstantValue.StructType].ToString();
					pumpdata.StrFitType = dataRow[ConstantValue.FixType].ToString();
					pumpdata.DQ = Convert.ToDouble(dataRow[ConstantValue.Volume].ToString());
					pumpdata.DH = Convert.ToDouble(dataRow[ConstantValue.Head].ToString());
					pumpdata.DEffection = Convert.ToDouble(dataRow[ConstantValue.Efficient].ToString());
					pumpdata.NRotSpeed = Convert.ToInt32(dataRow[ConstantValue.RotSpeed].ToString());
					pumpdata.DPumpPower = Convert.ToDouble(dataRow[ConstantValue.Power].ToString());
					pumpdata.DIn = Convert.ToDouble(dataRow[ConstantValue.InPipeDN].ToString());
					pumpdata.DOut = Convert.ToDouble(dataRow[ConstantValue.OutPipeDN].ToString());
					pumpdata.DEnginPower = Convert.ToDouble(dataRow[ConstantValue.EnginPower].ToString());
					pumpdata.StrEnginSpecfication = dataRow[ConstantValue.EnginSPEC].ToString();
					pumpdata.DVoltage = Convert.ToDouble(dataRow[ConstantValue.Voltage].ToString());
					pumpdata.StrImpellerPos = dataRow[ConstantValue.ImpellerPos].ToString();
					pumpdata.DMaxPressure = Convert.ToDouble(dataRow[ConstantValue.MaxPressure].ToString());
					pumpdata.StrUse = dataRow[ConstantValue.Use].ToString();
					pumpdata.BDrawbyself = Convert.ToBoolean(dataRow[ConstantValue.IsDrawSelf].ToString());
					pumpdata.BResistcorrosion = Convert.ToBoolean(dataRow[ConstantValue.ResistCorrode].ToString());
					pumpdata.StrMaterial = dataRow[ConstantValue.Material].ToString();
					pumpdata.DWeight = Convert.ToDouble(dataRow[ConstantValue.Weight].ToString());
					pumpdata.DMinTemperature = Convert.ToDouble(dataRow[ConstantValue.MinTemp].ToString());
					pumpdata.DMaxTemperature = Convert.ToDouble(dataRow[ConstantValue.MaxTemp].ToString());
					pumpdata.dVacuum = Convert.ToDouble(dataRow[ConstantValue.MaxDrawHeight].ToString());
					pumpdata.dHeight = Convert.ToDouble(dataRow[ConstantValue.Height].ToString());
					pumpdata.dLength = Convert.ToDouble(dataRow[ConstantValue.Length].ToString());
					pumpdata.dWidth = Convert.ToDouble(dataRow[ConstantValue.Width].ToString());
					pumpdata.strFlowtype = dataRow[ConstantValue.Volume].ToString();
					pumpdata.bPefectSeal = Convert.ToBoolean(dataRow[ConstantValue.PefectSealed].ToString());
					pumpdata.bExplosionProof = Convert.ToBoolean(dataRow[ConstantValue.ExposeProof].ToString());
					pumpdata.strFitRvtName = dataRow[ConstantValue.FitRvtName].ToString();
					pumpdata.strBaseParam = dataRow[ConstantValue.BaseParam].ToString();
					pumpdata.strDampBaseParam = dataRow[ConstantValue.DampBaseParam].ToString();
					pumpdata.stringHCurve = dataRow[ConstantValue.H_QCurve].ToString();
					pumpdata.stringNCurve = dataRow[ConstantValue.N_QCurve].ToString();
					pumpdata.stringECurve = dataRow[ConstantValue.E_QCurve].ToString();
					pumpdata.stringNPSHCurve = dataRow[ConstantValue.NPSHorHs_QCurve].ToString();
					pumpdata.nHCurveIndex = Convert.ToInt32(dataRow[ConstantValue.H_ExpIndex].ToString());
					pumpdata.nNCurveIndex = Convert.ToInt32(dataRow[ConstantValue.N_ExpIndex].ToString());
					pumpdata.nECurveIndex = Convert.ToInt32(dataRow[ConstantValue.E_ExpIndex].ToString());
					pumpdata.nNPSHCurveIndex = Convert.ToInt32(dataRow[ConstantValue.NPSH_ExpIndex].ToString());
					pumpdata.strFamily = factory.szFamily;
					pumpdata.strImage2D = Path.Combine(Product.FamilyImageLocation, "Pump", factory.szImage2D);
					pumpdata.strImage3D = Path.Combine(Product.FamilyImageLocation, "Pump", factory.szImage3D);
					return true;
				}
			}
			return false;
		}

		public static HYPumpBase GetPumpBaseDataById(string id, string tablename)
		{
			HYPumpBase hypumpBase = new HYPumpBase();
			foreach (object obj in new HYPumpDataReader().GetPumpDataTable(tablename).Rows)
			{
				DataRow dataRow = (DataRow)obj;
				if (dataRow["Id"].ToString() == id)
				{
					hypumpBase.id = id;
					hypumpBase.familyname = dataRow["FamilyName"].ToString();
					hypumpBase.dampfamilyname = dataRow["DampFamilyName"].ToString();
					hypumpBase.image = Path.Combine(Product.FamilyImageLocation, "PumpBase", dataRow["Image"].ToString());
					hypumpBase.dampimage = Path.Combine(Product.FamilyImageLocation, "PumpBase", dataRow["DampImage"].ToString());
					break;
				}
			}
			return hypumpBase;
		}

		public static string GetTheUniqueIdentifier(string szTableName, string szSepc)
		{
			return string.Format("{0}_{1}", szTableName, szSepc);
		}

		public static Dictionary<string, string> ConvertStringToDic(string str)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			if (string.IsNullOrEmpty(str))
			{
				return dictionary;
			}
			string[] array = str.Split(new string[]
			{
				"((",
				")(",
				"))",
				";"
			}, StringSplitOptions.RemoveEmptyEntries);
			if (array.Count<string>() > 1)
			{
				string[] array2 = array;
				for (int i = 0; i < array2.Length; i++)
				{
					string[] array3 = array2[i].Split(new string[]
					{
						" ",
						"="
					}, StringSplitOptions.RemoveEmptyEntries);
					if (array3.Count<string>() == 2)
					{
						dictionary.Add(array3[0], array3[1]);
					}
				}
			}
			return dictionary;
		}

		public static List<HYPumpData> GetAllPumpData()
		{
			List<HYPumpData> list = new List<HYPumpData>();
			foreach (HYPumpFactory pumpFactory in HYPumpDataManager.GetAllFactory())
			{
				list.AddRange(HYPumpDataManager.GetTablePumpData(pumpFactory));
			}
			return list;
		}

		public static List<HYPumpData> GetTablePumpData(HYPumpFactory pumpFactory)
		{
			List<HYPumpData> list = new List<HYPumpData>();
			DataTable pumpDataTable = new HYPumpDataReader().GetPumpDataTable(pumpFactory.szTableName);
			string szTableName = HYPumpDataManager.GetFactory(pumpFactory.szFactory, pumpFactory.szTypeInFactory).szTableName;
			foreach (object obj in pumpDataTable.Rows)
			{
				DataRow dataRow = (DataRow)obj;
				HYPumpData hypumpData = new HYPumpData();
				hypumpData.strIdentifier = HYPumpDataManager.GetTheUniqueIdentifier(szTableName, hypumpData.StrSpecification);
				hypumpData.StrFactory = pumpFactory.szFactory;
				hypumpData.StrTypeInFactory = pumpFactory.szTypeInFactory;
				hypumpData.StrSpecification = dataRow[ConstantValue.SPEC].ToString();
				hypumpData.StrBaseType = dataRow[ConstantValue.Type].ToString();
				hypumpData.StrStructType = dataRow[ConstantValue.StructType].ToString();
				hypumpData.StrFitType = dataRow[ConstantValue.FixType].ToString();
				hypumpData.DQ = Convert.ToDouble(dataRow[ConstantValue.Volume].ToString());
				hypumpData.DH = Convert.ToDouble(dataRow[ConstantValue.Head].ToString());
				hypumpData.DEffection = Convert.ToDouble(dataRow[ConstantValue.Efficient].ToString());
				hypumpData.NRotSpeed = Convert.ToInt32(dataRow[ConstantValue.RotSpeed].ToString());
				hypumpData.DPumpPower = Convert.ToDouble(dataRow[ConstantValue.Power].ToString());
				hypumpData.DIn = Convert.ToDouble(dataRow[ConstantValue.InPipeDN].ToString());
				hypumpData.DOut = Convert.ToDouble(dataRow[ConstantValue.OutPipeDN].ToString());
				hypumpData.DEnginPower = Convert.ToDouble(dataRow[ConstantValue.EnginPower].ToString());
				hypumpData.StrEnginSpecfication = dataRow[ConstantValue.EnginSPEC].ToString();
				hypumpData.DVoltage = Convert.ToDouble(dataRow[ConstantValue.Voltage].ToString());
				hypumpData.StrImpellerPos = dataRow[ConstantValue.ImpellerPos].ToString();
				hypumpData.DMaxPressure = Convert.ToDouble(dataRow[ConstantValue.MaxPressure].ToString());
				hypumpData.StrUse = dataRow[ConstantValue.Use].ToString();
				hypumpData.BDrawbyself = Convert.ToBoolean(dataRow[ConstantValue.IsDrawSelf].ToString());
				hypumpData.BResistcorrosion = Convert.ToBoolean(dataRow[ConstantValue.ResistCorrode].ToString());
				hypumpData.StrMaterial = dataRow[ConstantValue.Material].ToString();
				hypumpData.DWeight = Convert.ToDouble(dataRow[ConstantValue.Weight].ToString());
				hypumpData.DMinTemperature = Convert.ToDouble(dataRow[ConstantValue.MinTemp].ToString());
				hypumpData.DMaxTemperature = Convert.ToDouble(dataRow[ConstantValue.MaxTemp].ToString());
				hypumpData.dVacuum = Convert.ToDouble(dataRow[ConstantValue.MaxDrawHeight].ToString());
				hypumpData.dHeight = Convert.ToDouble(dataRow[ConstantValue.Height].ToString());
				hypumpData.dLength = Convert.ToDouble(dataRow[ConstantValue.Length].ToString());
				hypumpData.dWidth = Convert.ToDouble(dataRow[ConstantValue.Width].ToString());
				hypumpData.strFlowtype = dataRow[ConstantValue.Volume].ToString();
				hypumpData.bPefectSeal = Convert.ToBoolean(dataRow[ConstantValue.PefectSealed].ToString());
				hypumpData.bExplosionProof = Convert.ToBoolean(dataRow[ConstantValue.ExposeProof].ToString());
				hypumpData.strFitRvtName = dataRow[ConstantValue.FitRvtName].ToString();
				hypumpData.strBaseParam = dataRow[ConstantValue.BaseParam].ToString();
				hypumpData.strDampBaseParam = dataRow[ConstantValue.DampBaseParam].ToString();
				hypumpData.stringHCurve = dataRow[ConstantValue.H_QCurve].ToString();
				hypumpData.stringNCurve = dataRow[ConstantValue.N_QCurve].ToString();
				hypumpData.stringECurve = dataRow[ConstantValue.E_QCurve].ToString();
				hypumpData.stringNPSHCurve = dataRow[ConstantValue.NPSHorHs_QCurve].ToString();
				hypumpData.nHCurveIndex = Convert.ToInt32(dataRow[ConstantValue.H_ExpIndex].ToString());
				hypumpData.nNCurveIndex = Convert.ToInt32(dataRow[ConstantValue.N_ExpIndex].ToString());
				hypumpData.nECurveIndex = Convert.ToInt32(dataRow[ConstantValue.E_ExpIndex].ToString());
				hypumpData.nNPSHCurveIndex = Convert.ToInt32(dataRow[ConstantValue.NPSH_ExpIndex].ToString());
				hypumpData.strFamily = pumpFactory.szFamily;
				hypumpData.strImage2D = Path.Combine(Product.FamilyImageLocation, "Pump", pumpFactory.szImage2D);
				hypumpData.strImage3D = Path.Combine(Product.FamilyImageLocation, "Pump", pumpFactory.szImage3D);
				list.Add(hypumpData);
			}
			return list;
		}

		private List<HYPumpData> listPumpData = new List<HYPumpData>();
	}
}
